package com.arvato.right.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.right.bean.bean_do.WxworkRightRoleDo;
import com.arvato.right.bean.bean_do.WxworkRightRoleResourceDo;
import com.arvato.right.bean.bean_vo.WxworkRightResourceVo;
import com.arvato.right.bean.bean_vo.WxworkRightRoleVo;
import com.arvato.right.mapper.WxworkRightRoleMapper;
import com.arvato.right.mapper.WxworkRightRoleResourceMapper;
import com.arvato.right.mapper.WxworkRightUserRoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


@Service
@Transactional
public class WxworkRightRoleService {
	@Autowired
	private WxworkRightRoleMapper wxworkRightRoleMapper;
	@Autowired
	private WxworkRightRoleResourceMapper wxworkRightRoleResourceMapper;
	@Autowired
	private WxworkRightUserRoleMapper wxworkRightUserRoleMapper;
	@Autowired
	private WxworkRightResourceService resourceService;
	/**
	 * 插入角色
	 * @param role
	 * @param resourceIds
	 * @return
	 */
	public int insert(WxworkRightRoleDo role, String[] resourceIds) {
		// 先插入角色 
		int row = wxworkRightRoleMapper.insert(role);
		if(row == 1 && resourceIds != null){
			// 插入成功 再给角色配置资源
			for(String resourceId : resourceIds){
				WxworkRightRoleResourceDo roleResource = new WxworkRightRoleResourceDo();
				roleResource.setCreateDate(new Date());
				roleResource.setResourceId(resourceId);
				roleResource.setRoleId(role.getRoleId());
				wxworkRightRoleResourceMapper.insert(roleResource);
			}
		}
		return row;
	}
	/**
	 * 查询角色列表
	 * @param query
	 * @return
	 */
	public PageData listRoleByQuery(Query query) {
		// 设置分页参数
		PageHelper.startPage(query.getPageNum(), query.getPageSize());
		// 执行查询
		List<WxworkRightRoleVo> userList = wxworkRightRoleMapper.listRoleByQuery(query);
		// 得到结果
		PageInfo<WxworkRightRoleVo> pageInfo = new PageInfo<WxworkRightRoleVo>(userList);
		List<WxworkRightRoleVo> list = pageInfo.getList();
		for(WxworkRightRoleVo roleVo : list) {
			// 查询角色所拥有的资源
			List<WxworkRightResourceVo> resourceList = resourceService.listResourceByRoleId(roleVo.getRoleId());
			roleVo.setResourceList(resourceList);
		}
		PageData data = new PageData(pageInfo.getTotal(), list);
		return data;
	}
	/**
	 * 查询一个人的所有角色
	 * @param userId
	 * @return
	 */
	public List<WxworkRightRoleVo> listRoleByUser(String userId){
		return wxworkRightRoleMapper.listRoleByUserId(userId);
	}
	/**
	 * 查询单个角色
	 * @param query
	 * @param withResource false不查询这个角色的资源 true 查询资源
	 * @return
	 */
	public WxworkRightRoleVo getRoleByQuery(Query query, boolean withResource) {
		WxworkRightRoleVo role = wxworkRightRoleMapper.getRoleByQuery(query);
		if(withResource){
			// 查询这个角色有哪些资源
			List<WxworkRightResourceVo> resourceList = resourceService.listResourceByRoleId(role.getRoleId());
			role.setResourceList(resourceList);
		}
		return role;
	}
	/**
	 * 修改角色
	 * @param role
	 * @return
	 */
	public int update(WxworkRightRoleDo role, String[] resourceIds) {
		Query query = new Query();
		query.put("roleId", role.getRoleId());
		int row = wxworkRightRoleMapper.update(role);
		if(row == 1){
			// 删除之前的角色和资源的关联关系
			wxworkRightRoleResourceMapper.deleteByQuery(query);
			// 配置新的关联关系
			for(String resourceId : resourceIds){
				WxworkRightRoleResourceDo roleResource = new WxworkRightRoleResourceDo();
				roleResource.setCreateDate(new Date());
				roleResource.setResourceId(resourceId);
				roleResource.setRoleId(role.getRoleId());
				wxworkRightRoleResourceMapper.insert(roleResource);
			}
		}
		return row;
	}
	/**
	 * 根据Id删除角色
	 * @param valueOf
	 * @return
	 */
	public int delete(String roleId) {
		int row = wxworkRightRoleMapper.delete(roleId);
		if(row == 1){
			Query query = new Query();
			query.put("roleId", roleId);
			// 删除角色和资源的对应关系
			wxworkRightRoleResourceMapper.deleteByQuery(query);
			// 删除角色和人员的对应关系
			wxworkRightUserRoleMapper.deleteByQuery(query);
		}
		return row;
	}
	/**
	 * 根据Id查询角色
	 * @param roleId
	 * @return
	 */
	public WxworkRightRoleVo getRoleById(String roleId) {
		WxworkRightRoleVo role = wxworkRightRoleMapper.getRoleById(roleId);
		List<WxworkRightResourceVo> resourceList = resourceService.listResourceByRoleId(role.getRoleId());
		role.setResourceList(resourceList);
		return role;
	}
	
}
